package com.sxt.business.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxt.business.domain.Goods;
import com.sxt.business.domain.Provider;
import com.sxt.business.mapper.GoodsMapper;
import com.sxt.business.service.IGoodsService;
import com.sxt.business.service.IProviderService;
import com.sxt.business.vo.GoodsVO;
import com.sxt.system.constast.Constast;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.FileUploadAndDownUtil;
import com.sxt.system.utils.WebAppUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-15
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

	
	@Override
	public DataGridView queryAllGoods(GoodsVO goodsVO) {
		IPage<Goods> page = new Page<>(goodsVO.getPage(), goodsVO.getLimit());  
		
		QueryWrapper<Goods> qw = new QueryWrapper<Goods>();
		qw.eq(goodsVO.getProviderid()!=null, "providerid", goodsVO.getProviderid());
		qw.like(goodsVO.getGoodsname()!=null, "goodsname", goodsVO.getGoodsname());
		qw.like(goodsVO.getProductcode()!=null, "productcode", goodsVO.getProductcode());
		qw.like(goodsVO.getPromitcode()!=null, "promitcode", goodsVO.getPromitcode());
		qw.like(goodsVO.getDescription()!=null, "description", goodsVO.getDescription());
		qw.like(goodsVO.getSize()!=null, "size", goodsVO.getSize());
		this.getBaseMapper().selectPage(page, qw);
		List<Goods> list = page.getRecords();
		for (Goods goods : list) {
			Integer providerid = goods.getProviderid();
			if(null!=providerid) {
				Provider provider = WebAppUtils.getContent().getBean(IProviderService.class).getById(providerid);
				goods.setProvidername(provider.getProvidername());
			}
		}
		return new DataGridView(page.getTotal(),list);
	}

	@Value("${upload.upload-root-path}")
	private String uploadRootPath="D:/upload/";
	
	@Override
	public Goods addGoods(Goods goods) {
		//处理图片
		String imgpath = goods.getGoodsimg();
		if(!imgpath.equals(Constast.GOODS_DEFAULT_IMAGE)) {
			if(imgpath.endsWith("_temp")) {
				String path = FileUploadAndDownUtil.changeFileName(uploadRootPath,imgpath);
				goods.setGoodsimg(path);
			}
		}
		this.getBaseMapper().insert(goods);
		return goods;
	}

	@Override
	public Goods updateGoods(Goods goods) {
		//处理图片
		String imgpath = goods.getGoodsimg();
		if(!imgpath.equals(Constast.GOODS_DEFAULT_IMAGE)) {
			if(imgpath.endsWith("_temp")) {
				String path = FileUploadAndDownUtil.changeFileName(uploadRootPath,imgpath);
				goods.setGoodsimg(path);
				//删除老照片
				Goods oldGoods = this.getBaseMapper().selectById(goods.getId());
				FileUploadAndDownUtil.deleteFile(uploadRootPath, oldGoods.getGoodsimg());
			}
		}
		this.getBaseMapper().updateById(goods);
		return goods;
	}

	@Override
	public boolean removeById(Serializable id) {
		//删除图片
		Goods goods = this.getBaseMapper().selectById(id);
		String imgpath = goods.getGoodsimg();
		if(!imgpath.equals(Constast.GOODS_DEFAULT_IMAGE)) {
			//删除图片
			FileUploadAndDownUtil.deleteFile(uploadRootPath, goods.getGoodsimg());
		}
		return super.removeById(id);
	}

	@Override
	public boolean removeByIds(Collection<? extends Serializable> idList) {
		for (Serializable serializable : idList) {
			this.removeById(serializable);
		}
		return true;
	}

	@Override
	public DataGridView queryAllAvailableGoods() {
		QueryWrapper<Goods> qw = new QueryWrapper<Goods>();
		qw.eq("available", Constast.AVAILABLE_TRUE);
		return new DataGridView(this.getBaseMapper().selectList(qw));
	}

	@Override
	public Goods getById(Serializable id) {
		return super.getById(id);
	}

	@Override
	public DataGridView loadGoodsByProviderid(Integer providerid) {
		QueryWrapper<Goods> qw = new QueryWrapper<Goods>();
		qw.eq("available", Constast.AVAILABLE_TRUE);
		qw.eq(providerid!=null, "providerid", providerid);
		return new DataGridView(this.getBaseMapper().selectList(qw));
	}

	
	
}
